home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- * isp2hex.c *
- * ispCODE to Convert an ispSTREAM File Into ispHEX Array. *
- * Lattice Semiconductor Corp. Copyright 1996,1997 *
- * *
- * V1.01 Howard Tang 11/19/96 Change count from unsigned int to long int. *
- * V1.02 Howard Tang 11/25/96 Correct size of buffer. *
- * Fix the output file name to be istream.hex *
- * V1.03 Howard Tang 6/23/97 Fix the large (>64K bytes) .isp file problem. *
- *****************************************************************************/
-
- #include <stdio.h>
- #include <dos.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- short int main(short int argc, char *argv[])
- {
- FILE *fpr,*fpw;
- char *ptr,
- filename[300],
- str[300];
- unsigned char data;
- /* V1.03 unsigned short*/ long int temp,temp1; /*V1.02 Add temp1*/
- long int count; /*V1.01 the byte size of the ispSTREAM file*/
-
- printf("\n Lattice Semiconductor Corp.\n");
- printf("\n ispSTREAM to HEX String Converter V1.03. Copyright 1996,1997.\n");
-
- if ((argc < 2) || (argc > 2)) {
- printf("\nUsage: isp2hex [drive:][path]isp_filename\n");
- printf("Example: isp2hex my_file.isp\n");
- printf("\n");
- printf("isp_filename The ispSTREAM File Created By dld2isp.exe \n");
- exit(1);
- }
- if ((fpr = fopen(argv[1], "rb")) == NULL) {
- printf("\n%s File not found!\n", argv[1]);
- exit(1);
- }
- strcpy(str, argv[1]);
- ptr = strchr(str, '.');
- if (ptr)
- *ptr = '\0';
- sprintf(filename, "%s.isp", str);
- strcpy(str, argv[1]);
- if (stricmp(str, filename)) {
- printf("\nFile %s Is Not An ispSTREAM File\n", str);
- exit(1);
- }
- strcpy(str,argv[1]);
- ptr=strchr(str,'.');
- if (ptr)
- *ptr='\0';
- /* sprintf(filename,"%s.hex",str); */
- sprintf(filename,"istream.hex"); /*V1.02 fix the output file name*/
- if ((fpw = fopen(filename, "w")) == NULL) {
- printf("\n%s File not found!\n", filename);
- exit(1);
- }
- data = fgetc(fpr);
- /*Check the ispSTREAM file type*/
- count = 1;
- if (data==0x00) fprintf(fpw,"\n/*SLIM ispSTREAM File on ISP programming.*/");
- else if (data==0x01)
- fprintf(fpw,"\n/*SLIM ispSTREAM File on ispJTAG programming.*/");
- else if (data==0x02)
- fprintf(fpw,"\n/*Super SLIM ispSTREAM File on ISP programming.*/");
- else if (data==0x03)
- fprintf(fpw,"\n/*Super SLIM ispSTREAM File on ispJTAG programming.*/");
- else if (data==0x0F)
- fprintf(fpw,"\n/*ispSTREAM File on ISP programming.*/");
- else if (data==0xF0)
- fprintf(fpw,"\n/*ispSTREAM File on ispJTAG programming.*/");
- else if (data==0x0A)
- fprintf(fpw,"\n/*super ispSTREAM File on ISP programming.*/");
- else if (data==0x0B)
- fprintf(fpw,"\n/*super ispSTREAM File on ispJTAG programming.*/");
- else {printf("\nFailed: The ispSTREAM File (%s) is not Valid for ispCODE.\n",argv[1]);
- fclose(fpr);
- exit(1);}
- /*Read the header information*/
- if ((data>=0x00)&&(data<=0x04)) temp =fgetc(fpr);
- else if ((data==0x0F)||(data==0xF0)) temp = fgetc(fpr) + 1;
- else temp = fgetc(fpr) + 1;
- /*ChainLength*/
- count++;
- fprintf(fpw,"\n/*The number of devices in the chain is %d.*/",temp);
- /*ErasePulse*/
- temp = fgetc(fpr) * 0x100;
- temp += fgetc(fpr);
- count += 2;
- fprintf(fpw,"\n/*The bulk erase time is %d mS.*/",temp);
- /*ProgramPulse*/
- fprintf(fpw,"\n/*The row programming time is %d mS.*/", fgetc(fpr));
- /*RowLength*/
- temp = fgetc(fpr) * 0x100;
- temp += fgetc(fpr);
- count += 2;
- if ((data>=0x00)&&(data<=0x04)) temp &= 0x0FFF; /*Mask out the control bits*/
- fprintf(fpw,"\n/*The number of rows for programming is %d.*/",temp);
- /*DataSize*/
- temp1 = fgetc(fpr) * 0x100; /*V1.02*/
- temp1 += fgetc(fpr); /*V1.02*/
- count += 2;
- fprintf(fpw,"\n/*The combined length of the data registers %d.*/",temp1);
- temp = fgetc(fpr) * 0x100;
- temp += fgetc(fpr);
- count += 2;
- if ((data>=0x00)&&(data<=0x04)) {
- /*IDStreamLength*/
- fprintf(fpw,"\n/*The combined length of all the ID registers %d.*/",temp);
- }
- else /* if ((data==0x0F)||(data==0xF0)) */ {
- /*EraseRowEnd*/
- fprintf(fpw,"\n/*The location where bulk erase verify end is %d.*/",temp);
- temp = 0; /*V1.02*/
- }
- if (temp1 >= temp) /*V1.02 allocate sufficient memory to process ID string
- or Data string*/
- fprintf(fpw,"\nunsigned char buffer[%d]; /*allocate memory to process the data*/\n",temp1/8+1);
- else
- fprintf(fpw,"\nunsigned char buffer[%d]; /*allocate memory to process the data*/\n",temp/8+1);
-
- fprintf(fpw,"\n");
- while (!feof(fpr))
- {fgetc(fpr);
- count++; /*count all the bytes till the end*/
- }
- fclose(fpr);
- /*restore the pointer to the beginning of the file*/
- fpr = fopen(argv[1],"rb");
- fprintf(fpw,"unsigned char ispstream[%ld] = {\n",count); /*v1.01*/
- for (temp=1; temp<count; temp++)
- { fprintf(fpw,"0x%02X,",fgetc(fpr));
- if (temp%15==0) fprintf(fpw,"\n"); /*make the file more readable*/
- }
- fprintf(fpw,"0x%02X",fgetc(fpr));
- fprintf(fpw,"};\n");
- fclose(fpw);
- fclose(fpr);
- printf("\nispHEX File (%s) Generated.\n",filename);
- return(0);
- }